package com.siblings.loan.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.lib.base.utils.thread.TimingThread
import com.siblings.loan.R
import com.siblings.loan.app.Apps
import com.siblings.loan.databinding.DialogLoanGuideBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.utils.event.SibEventType

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/8 15:50
 * @Annotation：贷款引导页 dialog
 */
class LoanGuideDialog: ProDialog,TimingThread.ITimingThreadListener {

    private lateinit var binding: DialogLoanGuideBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    private var timeThread: TimingThread? = null
    //等待时长
    private var countdown = 5

    override fun initWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.MATCH_PARENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window!!.setGravity(Gravity.CENTER)
    }

    constructor(context: Context?) : super(context) {}

    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogLoanGuideBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        startTimeThread()
    }

    override fun initListener() {
        setOnClickListener(R.id.textDuration) //两个按钮 左侧按钮
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    /**
     * 启动倒计时
     */
    @SuppressLint("SetTextI18n")
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        countdown = 5
        binding.textDuration.text = "$countdown s"
        binding.textDuration.isEnabled = false
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    @SuppressLint("SetTextI18n")
    override fun onLoading() {
        if (countdown <= 0){
            countdown = 5
            timeThread!!.stopThread()

            binding.textDuration.isEnabled = true
            binding.textDuration.text = Apps.app.getString(R.string.btn_ok)
            binding.textDuration.setTextColor(Color.parseColor("#FF4DB673"))
            binding.textDuration.setBackgroundResource(R.drawable.sib_radius_fill_white_12)
            return
        }
        binding.textDuration.isEnabled = false
        countdown--
        binding.textDuration.text = "$countdown s"
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.DIALOG.SIB_DIALOG_GUIDE_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addSibEventAction(SibEventType.DIALOG.SIB_DIALOG_GUIDE_DISMISS,eventRemark)
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
    }
}